﻿#ifndef __SS_CFG_DATA_H__
#define __SS_CFG_DATA_H__
#include "../ssBaseDefine.h"
#include <string>
#include <vector>
#include <map>


namespace StarSeeker
{

#define SS_CFG_DATA_INT8		1
#define SS_CFG_DATA_INT32		2
#define SS_CFG_DATA_STRING		3
#define SS_CFG_DATA_FLOAT		4
#define SS_CFG_DATA_INT32_V		5
#define SS_CFG_DATA_STRING_V	6
#define SS_CFG_DATA_FLOAT_V		7
#define SS_CFG_DATA_INT32_VV	8
#define SS_CFG_DATA_STRING_VV	9
#define SS_CFG_DATA_FLOAT_VV	10
#define SS_CFG_DATA_INT32_HEX	11

struct ssICfgValue
{
	ssICfgValue()
	{
		type = 0;
	}
	u8 type;

	virtual i32 GetI32() { return 0; }
};

struct ssCfgInt8 : public ssICfgValue
{
	ssCfgInt8()
	{
		type = SS_CFG_DATA_INT8;
		val = 0;
	}
	i8 val;

	virtual i32 GetI32()
	{
		return val;
	}
};

struct ssCfgInt32 : public ssICfgValue
{
	ssCfgInt32()
	{
		type = SS_CFG_DATA_INT32;
		val = 0;
	}
	i32 val;

	virtual i32 GetI32()
	{
		return val;
	}
};

struct ssCfgStr : public ssICfgValue
{
	ssCfgStr()
	{
		type = SS_CFG_DATA_STRING;
	}
	std::string val;
};

struct ssCfgFloat : public ssICfgValue
{
	ssCfgFloat()
	{
		type = SS_CFG_DATA_FLOAT;
		val = 0.0f;
	}
	f32 val;
};

struct ssCfgVInt32 : public ssICfgValue
{
	ssCfgVInt32()
	{
		type = SS_CFG_DATA_INT32_V;
	}
	std::vector<i32> val;
};

struct ssCfgVFloat : public ssICfgValue
{
	ssCfgVFloat()
	{
		type = SS_CFG_DATA_FLOAT_V;
	}
	std::vector<f32> val;
};

struct ssCfgVStr : public ssICfgValue
{
	ssCfgVStr()
	{
		type = SS_CFG_DATA_STRING_V;
	}
	std::vector<std::string> val;
};

struct ssCfgVVInt32 : public ssICfgValue
{
	ssCfgVVInt32()
	{
		type = SS_CFG_DATA_INT32_VV;
	}
	std::vector<std::vector<i32> > val;
};

struct ssCfgVVFloat : public ssICfgValue
{
	ssCfgVVFloat()
	{
		type = SS_CFG_DATA_FLOAT_VV;
	}
	std::vector<std::vector<f32> > val;
};

struct ssCfgVVStr : public ssICfgValue
{
	ssCfgVVStr()
	{
		type = SS_CFG_DATA_STRING_VV;
	}
	std::vector<std::vector<std::string> > val;
};

struct ssCfgInt32Hex : public ssICfgValue
{
	ssCfgInt32Hex()
	{
		type = SS_CFG_DATA_INT32_HEX;
		val = 0;
	}
	i32 val;

	virtual i32 GetI32()
	{
		return val;
	}
};

#define SS_CFG_DATA_BEGIN			void Register(){
#define SS_CFG_KEY1(key)			m_list.push_back(&key);m_key.push_back(&key);
#define SS_CFG_KEY2(key)			m_list.push_back(&key);m_key.push_back(&key);
#define SS_CFG_DATA(name)			m_list.push_back(&name);
#define SS_CFG_DATA_END(class_name)	} class_name(){Register();}

//双键行基类
class ssICfgRow
{
	//CFG_DATA_BEGIN
	//CFG_DATA_END
public:
	ssICfgRow();
	virtual~ssICfgRow();
	//按顺序获得变量
	ssICfgValue* GetValue();
	//获取键
	i64 GetKey();
	//清除临时数据
	void ClearTempData();
protected:
	std::vector<ssICfgValue*>	m_key;		//键必须是int
	std::vector<ssICfgValue*>	m_list;		//参数列表
private:
	u32							m_count;//读取计数器
};

//双键表基类
template<typename T>
class ssICfgTable
{
public:
	typedef typename std::map<i64, T>::const_iterator iterator;

	ssICfgTable()
	{
	}
	virtual~ssICfgTable()
	{
	}
	//读文件
	virtual bool Read() = 0;
	//查找数据
	const T* GetRow(i64 key)
	{
		iterator itr = m_map.find(key);
		if (itr != m_map.end())
		{
			return &itr->second;
		}
		return 0;
	}
	const T* GetRow(i32 key1, i32 key2)
	{
		i64 k = key1;
		k = (k << 32) | key2;
		return GetRow(k);
	}
	//遍历
	iterator MapBegin()
	{
		return m_map.begin();
	}
	iterator MapEnd()
	{
		return m_map.end();
	}
	u32 MapSize()
	{
		return m_map.size();
	}
protected:
	std::map<i64, T>	m_map;	//数据表
	std::string			m_path;	//文件路径
};


//使用例子
/*class TestCfgRow : public ssICfgRow
{
public:
	SS_CFG_DATA_BEGIN
	SS_CFG_KEY(m_id)
	SS_CFG_DATA(m_icon)
	SS_CFG_DATA(m_open)
	SS_CFG_DATA_END(TestCfgRow)

	ssCfgInt32	m_id;
	ssCfgStr	m_icon;
	ssCfgStr	m_open;
};
class TestCfg : public ssICfgTable<TestCfgRow>
{
public:
	TestCfg()
	{
		m_path = "cfg/FootIcon.xml";
	}
	bool Read()
	{
		return true;
	}
};*/

}
#endif
